﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument
{
    /// <summary>
    /// Desktop 的摘要说明。
    /// </summary>
    public partial class DesktopPage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            dgDocList.SelectedIndexChanged += dgDocList_SelectedIndexChanged;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //考勤操作
                DutyOperation();
                Bangding();
                bindtaskgrid();
            }

            base.OnLoad(e);
        }

        private void DutyOperation()
        {
            // Is the attendance feature enabled?
            if (UDSConfig.IsFeatureEnabled(194))
            {
                var now = DateTime.Now;
                var userId = SiteContext.Current.UserID;
                var dutyTime = WA_Setting.GetStaffDutyTime(userId, now);
                if (dutyTime.ActualOnDutyTime == DateTime.MinValue)
                {
                    // 未完成上班考勤
                    if (dutyTime.OnDutyTime.TimeOfDay >= now.TimeOfDay)
                    {
                        // 没有迟到
                        WA_Duty.RecordOnDutyData(userId, now, true, "");

                        //弹出新页面表示成功
                        Response.Write("<script language=javascript>window.open('../WorkAttendance/checksucessful.aspx?login=in','_blank','height=200,width=400,status=no,toolbar=no,menubar=no,location=no')</script>");
                    }
                    else
                    {
                        // 迟到了
                        // 跳转到填写理由页面
                        Response.Redirect("../WorkAttendance/Default.aspx?login=in");
                    }
                }

#if false
            var wd = new WA_Duty(SiteContext.Current.UserID);
            int Duty = wd.HaveCompletedDuty(DateTime.Now);
            //检查当天是否已经完成上班考勤
            if (Duty == -1)
            {
                if (wd.CheckStatus(DutyAction.OnDuty)) //没有迟到
                {
                    wd.RecordOnDutyData(DateTime.Now, true, "").ToString();
                    //弹出新页面表示成功
                    Response.Write(
                        "<script language=javascript>window.open('../WorkAttendance/checksucessful.aspx?login=in','_blank','height=200,width=400,status=no,toolbar=no,menubar=no,location=no')</script>");
                }
                else //迟到
                {
                    //跳转到填写理由页面
                    Response.Redirect("../WorkAttendance/Default.aspx?notnormal=1&login=in");
                }
            }
#endif
            }
        }

        public void bindtaskgrid()
        {
            var task = new Components.Task();
            DataTable mydb;
            using (SqlDataReader dr = task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), SiteContext.Current.UserName, 1))
            {
                mydb = Tools.ConvertDataReaderToDataTable(dr);
            }

            if (mydb.Rows.Count < 5)
            {
                // TODO:
                // What is this for?
                //
                //int tmp = 5 - mydb.Rows.Count;
                //for (int i = 0; i < tmp; i++)
                //{
                //    DataRow myDataRow = mydb.NewRow();
                //    myDataRow[0] = "-";
                //    mydb.Rows.Add(myDataRow);
                //}
            }
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            // setgrid();
            mydb.Dispose();
        }

        public string GetStatus(string str)
        {
            switch (str)
            {
                case "0":
                    return "?"; // 待定
                case "1":
                    return "!"; // 待办
                case "2":
                    return "√"; // 完成
                default:
                    return "";
            }
        }

        public string GetPeriodByPeriodID(string EndTime, string begintime, string endtime)
        {
            if (begintime == "0" && endtime == "0")
            {
                if (EndTime != "")
                {
                    return DateTime.Parse(EndTime).ToShortTimeString();
                }
                return "-";
            }
            if (begintime == "" && endtime == "")
            {
                return "-";
            }
            int b = Int32.Parse(begintime);
            int e = Int32.Parse(endtime);
            var dt = new DateTime(1999, 1, 1, 8, 0, 0, 0);
            var ts = new TimeSpan(0, 0, (b - 1) * 30, 0, 0);
            DateTime bt = dt.Add(ts);
            DateTime et = bt.Add(new TimeSpan(0, 0, (e - b + 1) * 30, 0, 0));
            return bt.ToShortTimeString() + "-" + et.ToShortTimeString();
        }

        public string GetDateString(string DateString)
        {
            if (DateString == "")
            {
                return "";
            }
            return DateTime.Parse(DateString).ToShortDateString();
        }

        public string GetRealName(string username)
        {
            if (username != "")
            {
                return Staff.GetRealNameByUsername(username);
            }
            return "";
        }

        public string GetType(string type)
        {
            string Type = "";
            if (type != "")
            {
                switch (Int32.Parse(type))
                {
                    case 1:
                        Type = "会议";
                        break;
                    case 2:
                        Type = "文案";
                        break;
                    case 3:
                        Type = "来访";
                        break;
                    case 4:
                        Type = "电话";
                        break;
                    case 5:
                        Type = "走访";
                        break;
                    case 6:
                        Type = "外出";
                        break;
                    case 7:
                        Type = "假期";
                        break;
                    case 8:
                        Type = "出差";
                        break;
                }
                return Type;
            }
            return "";
        }

        public string GetProjectName(string project)
        {
            if (project != "")
            {
                return ProjectClass.GetProjectName(Int32.Parse(project));
            }
            return "";
        }


        private void setgrid()
        {
            // foreach(DataGridItem dgi in this.dgList .Items)
            {
                // Label lb=(Label)(dgi.Cells[2].Controls[1]);
                // if(lb.Text=="?") dgi.BackColor=Color.AliceBlue ;
            }
        }

        private void dgDocList_SelectedIndexChanged(object sender, EventArgs e)
        { }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgList.CurrentPageIndex = e.NewPageIndex;
            var task = new Components.Task();
            DataTable mydb;
            using (SqlDataReader dr = task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), SiteContext.Current.UserName, 1))
            {
                mydb = Tools.ConvertDataReaderToDataTable(dr);
                dgList.DataSource = mydb.DefaultView;
                dgList.DataBind();
            }
            setgrid();
        }

        #endregion

        #region 绑定DBGRID

        private void Bangding()
        {
            var myDesktop = new Desktop();
            string UserName = SiteContext.Current.UserName;

            using (SqlDataReader dr = myDesktop.GetMyDocument(UserName, 10))
            {
                // TODO:
                // What is this for?
                //
                DataTable dt = Tools.ConvertDataReaderToDataTable(dr);
                //if (dt.Rows.Count < 5)
                //{
                //    int tmp = 5 - dt.Rows.Count;
                //    for (int i = 0; i < tmp; i++)
                //    {
                //        DataRow myDataRow = dt.NewRow();
                //        myDataRow[0] = "-";
                //        dt.Rows.Add(myDataRow);
                //    }
                //}
                dgDocList.DataSource = dt.DefaultView;
                dgDocList.DataBind();
            }

            // TODO:
            // The maile feature is disabled!
            //
            //dr = myDesktop.GetMyMail(UserName, 1);
            //dt = Tools.ConvertDataReaderToDataTable(dr);
            //if (dt.Rows.Count < 5)
            //{
            //    int tmp = 5 - dt.Rows.Count;
            //    for (int i = 0; i < tmp; i++)
            //    {
            //        DataRow myDataRow = dt.NewRow();
            //        myDataRow[0] = "-";
            //        //	myDataRow[4] = "";
            //        myDataRow[7] = "";
            //        dt.Rows.Add(myDataRow);
            //    }
            //}
            ////			DataView dv = dt.DefaultView;
            ////			dv.Sort = "MailSendDate Desc";
            //dgMailList.DataSource = dt.DefaultView;
            //dgMailList.DataBind();

            using (SqlDataReader dr = myDesktop.GetMyApprove(UserName, 2))
            {
                DataTable dt = Tools.ConvertDataReaderToDataTable(dr);
                if (dt.Rows.Count < 5)
                {
                    // TODO:
                    // What is this for?
                    //
                    //int tmp = 5 - dt.Rows.Count;
                    //for (int i = 0; i < tmp; i++)
                    //{
                    //    DataRow myDataRow = dt.NewRow();
                    //    myDataRow[0] = "-";
                    //    dt.Rows.Add(myDataRow);
                    //}
                }
                dgAppDocList.DataSource = dt.DefaultView;
                dgAppDocList.DataBind();
            }
        }

        #endregion
    }
}